/**   
* @Title: StrutsFileUpAction.java 
* @Package com.action 
* @Description: TODO(文件上傳) 
* @author 安天池   
* @date 2017年6月22日 下午1:41:53 
* JDK版本：1.8
* @version V1.0   
*/
package com.action;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @ClassName: StrutsFileUpAction
 * @Description: TODO(Struts2文件上傳)
 * @author 安天池
 * @date 2017年6月22日 下午1:41:53
 * 
 */
public class StrutsFileUpAction extends ActionSupport {

	/**
	 * @Fields serialVersionUID : TODO(用一句话描述这个变量表示什么)
	 */
	private static final long serialVersionUID = 1L;

	 // 这里变量名 和 页面表单元素 name 属性一致
	private File upload;
	private String uploadContentType;// 上传文件的类型
	private String uploadFileName;// 上传文件的名字

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String saveFile()  {
		ActionContext context = ActionContext.getContext();

		try {
			if(upload == null){
				context.put("save_message", "没有文件");
				return SUCCESS;

			}
			String path = "F:/fileupload/" + uploadFileName;
			FileUtils.copyFile(upload, new File(path));
			System.out.println("保存文件：：成功");
			
			context.put("save_message", "保存成功");
			return SUCCESS;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			context.put("save_message", "保存失敗");
			return SUCCESS;
			//e.printStackTrace();
			
		}
		
		//return SUCCESS;

	}
}
